<?php

namespace App\Http\Controllers\Api\V1;

use App\B2BProject\Custom\Business\GoodsCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use MyProject\Proxies\__CG__\stdClass;

class CategoryController extends Controller
{
    /**
     * 获取所有分类
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index()
    {
        $globalClasses = GoodsCategory::getGlobalCategory();

        if (is_null($globalClasses)) {
            return success_return(null);
        }

        $data = [];

        foreach ($globalClasses as $class) {
            $data[] = [
                'id'       => (int)$class->id,
                'name'     => $class->name,
                'children' => call_user_func_array(function ($classes) {
                    if (is_null($classes)) {
                        return new stdClass();
                    }

                    $data = [];

                    foreach ($classes as $class) {
                        $data[] = [
                            'id'   => $class->id,
                            'name' => $class->name,
                        ];
                    }

                    return $data;
                }, [$class->children]),
            ];
        }

        return success_return($data);
    }
}
